/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Historial_evento.java
 *
 * Created on 16/09/2011, 18:35:07
 */
package visuales;

import entidades.Historial;
import entidades.TipoEvento;
import gestores.GestorProduccion;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Franco
 */
public class Historial_evento extends javax.swing.JFrame {

    private GestorProduccion gp;
    private DefaultTableModel dtm;
    private JFrame pantalla;

    /** Creates new form Historial_evento */
    public Historial_evento(JFrame padre) {
        initComponents();
        this.pantalla = padre;
        gp = new GestorProduccion();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        btnMostrar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtHistorial = new javax.swing.JTable();
        btnSalir = new javax.swing.JButton();
        txtCantidad = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        rbNacimiento = new javax.swing.JRadioButton();
        rbDestete = new javax.swing.JRadioButton();
        rbEnfermedad = new javax.swing.JRadioButton();
        rbMuerte = new javax.swing.JRadioButton();
        rbPeleteo = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        txtDiasAtras = new javax.swing.JTextField();
        rbAsignado = new javax.swing.JRadioButton();
        rbRalgro = new javax.swing.JRadioButton();
        rbCuracion = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Historiales por evento");

        btnMostrar.setText("Mostrar resultados");
        btnMostrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMostrarActionPerformed(evt);
            }
        });

        jtHistorial.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtHistorial);

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        jLabel1.setText("Cantidad de registros:");

        buttonGroup1.add(rbNacimiento);
        rbNacimiento.setSelected(true);
        rbNacimiento.setText("Nacimiento");

        buttonGroup1.add(rbDestete);
        rbDestete.setText("Destete");

        buttonGroup1.add(rbEnfermedad);
        rbEnfermedad.setText("Enfermedad");

        buttonGroup1.add(rbMuerte);
        rbMuerte.setText("Muerte");

        buttonGroup1.add(rbPeleteo);
        rbPeleteo.setText("Peleteo");

        jLabel2.setText("En los ultimos dias:");

        buttonGroup1.add(rbAsignado);
        rbAsignado.setText("Asignado a familia");

        buttonGroup1.add(rbRalgro);
        rbRalgro.setText("Ralgro");

        buttonGroup1.add(rbCuracion);
        rbCuracion.setText("Curacion");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(627, Short.MAX_VALUE)
                .addComponent(btnSalir)
                .addGap(61, 61, 61))
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(489, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rbNacimiento)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rbDestete)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rbEnfermedad)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rbMuerte)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rbPeleteo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rbAsignado)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rbRalgro)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rbCuracion))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(31, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtDiasAtras, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 210, Short.MAX_VALUE)
                        .addComponent(btnMostrar, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(133, 133, 133))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbNacimiento)
                    .addComponent(rbDestete)
                    .addComponent(rbEnfermedad)
                    .addComponent(rbMuerte)
                    .addComponent(rbPeleteo)
                    .addComponent(rbAsignado)
                    .addComponent(rbRalgro)
                    .addComponent(rbCuracion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtDiasAtras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnMostrar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(btnSalir))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnMostrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMostrarActionPerformed
        this.armarTabla();
        this.mostrarDatos(this.getEvento());
    }//GEN-LAST:event_btnMostrarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
        pantalla.setVisible(true);
    }//GEN-LAST:event_btnSalirActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new Historial_evento().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnMostrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtHistorial;
    private javax.swing.JRadioButton rbAsignado;
    private javax.swing.JRadioButton rbCuracion;
    private javax.swing.JRadioButton rbDestete;
    private javax.swing.JRadioButton rbEnfermedad;
    private javax.swing.JRadioButton rbMuerte;
    private javax.swing.JRadioButton rbNacimiento;
    private javax.swing.JRadioButton rbPeleteo;
    private javax.swing.JRadioButton rbRalgro;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtDiasAtras;
    // End of variables declaration//GEN-END:variables

    private void armarTabla() {
        dtm = new DefaultTableModel();
        dtm.addColumn("Nombre");
        dtm.addColumn("Nombre del padre");
        dtm.addColumn("Nombre de la madre");
    }

    private void mostrarDatos(int evento) {
        ArrayList res_animales = null;
        switch (evento) {
            case TipoEvento.NACIMIENTO:
                dtm.addColumn("Fecha de nacimiento");
                break;
            case TipoEvento.MUERTE:
                dtm.addColumn("Fecha de muerte");
                break;
            case TipoEvento.DESTETE:
                dtm.addColumn("Fecha de destete");
                break;
            case TipoEvento.ENFERMEDAD:
                dtm.addColumn("Fecha de enfermedad");
                break;
            case TipoEvento.PELETEO:
                dtm.addColumn("Fecha de peleteo");
                break;
            case TipoEvento.ASIGNAR:
                dtm.addColumn("Fecha de asignacion a familia");
                break;
            case TipoEvento.RALGRO:
                dtm.addColumn("Fecha de ralgro");
                break;
            case TipoEvento.CURACION:
                dtm.addColumn("Fecha de curacion");
                break;
        }
        jtHistorial.setModel(dtm);
        int dias = 90;
        if (txtDiasAtras.getText().compareTo("") != 0) {
            dias = new Integer(txtDiasAtras.getText());
            System.out.println("Cantidad de dias ingresados por el usuario: " + dias);
        } else {
            JOptionPane.showMessageDialog(null, "Se toma como defecto los ultimos 90 dias", "Atencion", JOptionPane.INFORMATION_MESSAGE);
        }
        res_animales = gp.getHistoriales(evento, dias);
        this.mostrarHistorial(res_animales);
    }

    private int getEvento() {
        int evento = -1;
        if (rbDestete.isSelected()) {
            evento = TipoEvento.DESTETE;
        }
        if (rbEnfermedad.isSelected()) {
            evento = TipoEvento.ENFERMEDAD;
        }
        if (rbNacimiento.isSelected()) {
            evento = TipoEvento.NACIMIENTO;
        }
        if (rbMuerte.isSelected()) {
            evento = TipoEvento.MUERTE;
        }
        if (rbPeleteo.isSelected()) {
            evento = TipoEvento.PELETEO;
        }
        if (rbAsignado.isSelected()) {
            evento = TipoEvento.ASIGNAR;
        }
        if (rbRalgro.isSelected()) {
            evento = TipoEvento.RALGRO;
        }
        if (rbCuracion.isSelected()) {
            evento = TipoEvento.CURACION;
        }
        return evento;
    }

    private void mostrarHistorial(ArrayList res_animales) {
        if (res_animales != null || res_animales.isEmpty() == false) {
            txtCantidad.setText(String.valueOf(res_animales.size()));
            Iterator iter_historial = res_animales.iterator();
            Historial res_h;
            Object fila[] = new Object[4];
            while (iter_historial.hasNext()) {
                res_h = (Historial) iter_historial.next();
                fila[3] = res_h.getFecha().dateValue();
                fila[0] = res_h.getAnimal().getNombre();
                fila[1] = res_h.getAnimal().getPadre().getNombre();
                fila[2] = res_h.getAnimal().getMadre().getNombre();
                dtm.addRow(fila);
            }
            System.out.println("Grande pelusa!");
        }
    }
}
